package com.dataseek.iot.mysql.entity;


import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import static java.util.stream.Collectors.toList;

@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class LoginUser implements UserDetails {

    /**
     * 用户名
     */private String username;

    /**
     * 密码
     */
    private String password;

    /**
     * 用户ID
     */
    private Long userId;

    /**
     * 昵称
     */
    private String nickname;

    /**
     * 电话
     */
    private String tel;

    /**
     * 邮箱
     */
    private String mail;

    /**
     * 角色编码
     */
    private String roleId;


    /**
     * 创建时间
     */
    private String createTime;

    /**
     * 更新时间
     */
    private String updateTime;

    /**
     * 补充信息
     */
    private String comment;

    /**
     * 性别
     */
    private byte sex;

    /**
     * 头像
     */
    private String avatar;

    /**
     * 启用
     */
    private byte used;

    /**
     * 角色名称
     */
    private String roleName;

    /**
     * 角色编码
     */
    private String roleCode;

    /**
     *  权限信息
     */
    private String menuAuth;

    /**
     * 路由信息
     */
    private String routes;

    /**
     * 线路ID
     */
    private String lineCode;

    /**
     * 线路名称
     */
    private String lineName;


    /**
     * 区域
     */
    private List<Object> area;




    public LoginUser() {
    }

    public LoginUser( String username,  String password, List<String> roles) {
        this.username = username;
        this.password = password;
        this.roles = roles;
    }

    private List<String> roles = new ArrayList<>();

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.roles.stream().map(SimpleGrantedAuthority::new).collect(toList());
    }


    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }


    /**
     * 是否启用
     * @return
     */
    @Override
    public boolean isEnabled() {
        if(used==0){
            return false;
        }
        return true;
    }
}